/**
 * 
 */
package com.gko100.action;

import java.util.List;

import com.gko100.action.base.AdminBaseAction;
import com.gko100.model.AdminPerm;
import com.gko100.model.Administrator;
import com.gko100.model.Permission;
import com.gko100.model.User;
import com.gko100.model.UserPerm;

/**
 * @author Administrator
 *
 */
public class UserPermAction extends AdminBaseAction {
	
	private UserPerm userPerm;
	private Permission perm;
	private User user;
	private List<Permission> permList;
	private Integer[] permIdList;
	private List<UserPerm> userPermList;

	public UserPerm getUserPerm() {
		return userPerm;
	}

	public void setUserPerm(UserPerm userPerm) {
		this.userPerm = userPerm;
	}

	public Permission getPerm() {
		return perm;
	}

	public void setPerm(Permission perm) {
		this.perm = perm;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<Permission> getPermList() {
		return permList;
	}

	public void setPermList(List<Permission> permList) {
		this.permList = permList;
	}

	public Integer[] getPermIdList() {
		return permIdList;
	}

	public void setPermIdList(Integer[] permIdList) {
		this.permIdList = permIdList;
	}

	public List<UserPerm> getUserPermList() {
		return userPermList;
	}

	public void setUserPermList(List<UserPerm> userPermList) {
		this.userPermList = userPermList;
	}

	public String upload_userPerm(){
		
		if(this.getUser().getUserId()!=null){
			
			User u = adminManager.findUserById(this.getUser().getUserId());
			if(u!=null){
				
				this.setUser(u);
				List<UserPerm> userPermList = adminManager.findUserPermListByUser(u);
				if(userPermList!=null&&userPermList.size()>=1){
					
					this.setUserPermList(userPermList);
					Integer[] permIdList = new Integer[20];
					
					for(int i=0;i<userPermList.size();i++){
						
						if(userPermList.get(i).getPermValue()){
							
							permIdList[i]=userPermList.get(i).getPerm().getPermId();
							
						}
					}
					if(permIdList.length!=0){
						this.setPermIdList(permIdList);
					}
					
				}
				List<Permission> permList = adminManager.findPermList();
				if(permList!=null){
					this.setPermList(permList);
				}
				
			}

		}

		return "userPerm_upload_success";
	}
	
	public String modify_userPerm() throws Exception {
		
		if(this.getUser()!=null){
			adminManager.modify_userPermByUser(user, this.getPermIdList());
		}
		
		return "userPerm_modify_success";
	}
	
	
}
